package servlet;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求内容封装为Request对象（从 InputStream获取）
 */
public class Request {
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        while (count == 0) { //可能存在请求数据并没有即使送达 所以一直等待读到数据
            count = inputStream.available();//数据长度
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String inputStr = new String(bytes);
        String firstLine = inputStr.split("\\n")[0]; //GET / HTTP/1.1
        String[] strings = firstLine.split(" ");
        this.method = strings[0];
        this.url = strings[1];

        System.out.println("method = " + method);
        System.out.println("url = " + url);
    }

    public Request() {
    }

    private String method;//请求方式
    private String url;//地址
    private InputStream inputStream;//请求的输入流

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}
